﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Listen1.Entity;
using Listen1.WebRequest;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace WebApp.Controllers
{
    public class SongSheetController : Controller
    {
        Microsoft.Extensions.Caching.Memory.IMemoryCache Cache = null;
        JsonSerializerSettings settings = null;
        public SongSheetController(Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache) {
            Cache = memoryCache;
            settings = new Newtonsoft.Json.JsonSerializerSettings() { DateFormatString = "yyyy-MM-dd", MaxDepth = 1, ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() };
        }
        IList<SongSheet> _songSheetList = null;
        IList<SongSheet> SongSheetList {
            get {
                string key = "songSheetList";
                if (Cache.TryGetValue(key, out _songSheetList) && _songSheetList != null)
                {
                    return _songSheetList;
                }
                Listen1.WebRequest.NetEaseSongSheet sheet = new NetEaseSongSheet();
                _songSheetList = sheet.GetList(100);
                Cache.Set<IList<SongSheet>>(key, _songSheetList);
                return _songSheetList;
            }
        }

        SongSheet SongSheetDetail(string id)
        {
            string key = "songSheetDetail_" + id;
            SongSheet sheet = null;
            if (Cache.TryGetValue(key, out sheet) && sheet != null)
            {
                return sheet;
            }
            Listen1.WebRequest.NetEaseSongSheet sheetServ = new NetEaseSongSheet();
            SongSheet songSheet= SongSheetList.Where(s => s.Id.Equals(id)).FirstOrDefault();
            if (songSheet==null)
            {
                return null;
            }
            sheetServ.ReadSheetDetail(songSheet);
            sheetServ.ReadSongDetail(songSheet);
            Cache.Set<SongSheet>(key, songSheet);
            return songSheet;
        }


        public IActionResult List()
        {
            return Json(SongSheetList, settings);
        }

        public IActionResult Detail(string id)
        {
            SongSheet sheet = SongSheetDetail(id);
            return Json(new {
                SheetInfo=sheet,
                SongList=sheet.SongList
            }, settings);
        }

        public IActionResult SongDetail(string sheetId,string songId)
        {
            SongSheet songSheet= SongSheetDetail(sheetId);
            if (songSheet == null)
            {
                return null;
            }
            Song song = songSheet.SongList.Where(s => s.Id == songId).FirstOrDefault();
            if (song == null)
            {
                return null;
            }
            Listen1.WebRequest.NetEaseSongSheet sheet = new NetEaseSongSheet();            
            sheet.ReadSongDetail(song);
            sheet.Download(song,null);
            return Json(song, settings);
        }

    }
}